<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * user.php
 * @author ranuandra
 *
 * 24 Apr 2012 11:07:18
 *
 * 
 */

class Controller_User extends Kohana_Nasmoco
{
	public $auth_required = array('login', 'user');
	public function action_index()
	{
		//$this->cek_data();
		$user = Auth_ORM::instance()->get_user();
		$view = View::factory('user/index');
		//$view->data = $user->mstpelamar;
		$view->pelamar = View::factory('pelamar/index');
		$view->pelamar->data = $user->mstpelamar;
		//Pendidikan
		$view->pendidikan = View::factory('pendidikan/index');
		$view->pendidikan->datas = $user->mstpelamar->pelamarpendidikan->find_all();
		
		//kursus
		$view->kursus = View::factory('kursus/index');
		$view->kursus->datas = $user->mstpelamar->pelamarkursus->find_all();
		
		//pengalaman
		if($user->mstpelamar->levelpengalaman != 1)
		{
			$view->pengalaman = View::factory('pengalaman/form');
			$view->pengalaman->pelamar = $user->mstpelamar;
			$view->pengalaman->bidanglist = ORM::factory('mstbidang')->find_all()->as_array('id', 'bidang');
			$view->pengalaman->spesialisasilist = ORM::factory('mstspesialisasi')->find_all()->as_array('id', 'spesialisasi');
			$view->pengalaman->posisilist = ORM::factory('mstposisi')->find_all()->as_array('id', 'posisi');
			$view->pengalaman->industrilist = ORM::factory('mstindustri')->find_all()->as_array('id', 'industri_type');
			
		}
		else
		{
			$view->pengalaman = View::factory('pengalaman/index');
			$view->pengalaman->datas = $user->mstpelamar->pelamarpengalaman->order_by('sampai_tanggal', 'DESC')->find_all();
			
		}
		
		if($user->mstpelamar->flag_organisasi == null || $user->mstpelamar->flag_organisasi == 0)
		{
			$view->organisasi = View::factory('organisasi/form');
			$view->organisasi->flag_organisasi = $user->mstpelamar->flag_organisasi;

		}
		else 
		{
			
			$view->organisasi = View::factory('organisasi/index');
			$view->organisasi->datas = $user->mstpelamar->pglmnorganisasi->find_all();
		}
		
		//ketrampilan
		if($user->mstpelamar->flag_ketrampilan == null || $user->mstpelamar->flag_ketrampilan == 0)
		{
			$view->ketrampilan = View::factory('ketrampilan/form');
			$view->ketrampilan->flag_ketrampilan = $user->mstpelamar->flag_ketrampilan;
			$view->ketrampilan->lama = array('Dibawah 1 Tahun' => 'Dibawah 1 tahun', '1 tahun s/d 2 tahun' => '1 tahun s/d 2 tahun', '3 tahun s/d 5 tahun' => '3 tahun s/d 5 tahun', 'Diatas 5 tahun' => 'Diatas 5 tahun' );
			$view->ketrampilan->level = array('Pemula' => 'Pemula', 'Cukup Mahir' => 'Cukup Mahir', 'Mahir' => 'Mahir', 'Sangat Mahir' => 'Sangat Mahir');
		}
		else 
		{
			$view->ketrampilan = View::factory('ketrampilan/index');
			$view->ketrampilan->datas = $user->mstpelamar->pelamarketrampilan->find_all();
		}
		
		//bahasa
		$view->bahasa = View::factory('bahasa/index');
		$view->bahasa->datas = $user->mstpelamar->pelamarbahasa->find_all();
		
		//minat
		$view->minat = View::factory('minat/index');
		$view->minat->datas = $user->mstpelamar->pelamarminat->find_all();
		
		//reference
		$view->reference = View::factory('reference/index');
		$view->reference->datas = $user->mstpelamar->pelamarreference->find_all();
		
		//keluargakandung
		$view->keluargakandung = View::factory('keluargakandung/index');
		$view->keluargakandung->datas = $user->mstpelamar->pelamarkeluargakandung->find_all();
		
		$view->fotos = $user->mstpelamar->pelamarfoto->find_all();
		
		//keluarga
		$view->keluarga = View::factory('keluarga/index');
		$view->keluarga->datas = $user->mstpelamar->pelamarkeluarga->find_all();
		
		//konsep diri
		$view->konsepdiri = View::factory('konsepdiri/index');
		$view->konsepdiri->jawaban = $user->mstpelamar->pelamarkonsepdiri;
		$view->konsepdiri->pelamar = $user->mstpelamar;
		$view->konsepdiri->datas = ORM::factory('mstkonsepdiri')->find_all();
		
		//lain-lain
		$view->lainlain = View::factory('lainlain/index');
		$view->lainlain->jawaban = $user->mstpelamar->pelamarlainlain;
		$view->lainlain->pelamar = $user->mstpelamar;
		$view->lainlain->datas = ORM::factory('mstlainlain')->find_all();
		
		$view->data = $user;
		
		$this->template->container = $view;
	}
	

	public function action_logout()
	{
		$this->auto_render = false;
		Auth_ORM::instance()->logout();
		echo "<script>window.location.replace(\"".URL::base()."\")</script>";
		exit;
	}
	
	public function action_editphoto()
	{
		$this->auto_render = false;
		$view = View::factory('user/formphoto');
		echo $view;	
	}
	
	public function action_upload()
	{
		$db = Database::instance();
		$db->begin();
		if(isset($_FILES))
		{
			try 
			{
				if(isset($_FILES['photo']["name"]))
				{
					//echo $_FILES["fileimage"]["type"] ;
					if ($_FILES["photo"]["type"] == "image/gif")
					{
						$eks = "gif";
					}
					elseif($_FILES["photo"]["type"] == "image/jpeg")
					{
						$eks = "jpg";
					}
					elseif($_FILES["photo"]["type"] == "image/png")
					{
						$eks = "png";
					}
					$post = Validation::factory($_FILES);
	                $post ->rule('photo', 'not_empty')
	                      ->rule('photo', 'Upload::type', array(':value', array('png', 'jpeg', 'jpg')))
	                      ->rule('photo', 'Upload::size', array(':value', '500KiB'));
					if($post->check())
					{
						
						$fileName = Auth_ORM::instance()->get_user()->id.time().'.'.$eks;
						if($fphoto = Upload::save($post['photo'],$fileName, DOCROOT.'assets/photo/', 777))
						{
							//echo "test";	
							chmod(DOCROOT.'assets/photo/'.$fileName,0777);					
							Image::factory($fphoto)
									->resize(160,'',Image::WIDTH)
									->save(DOCROOT.'assets/photo/'.$fileName);
							
							$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
							$photo = ORM::factory('pelamarfoto');
							$photo->mstpelamar_id = $pelamar->id;
							$photo->filename = $fileName;
							$photo->created = date("Y-m-d H:i:s");
							$photo->save();
							$db->commit();
						}
						return Request::current()->redirect(URL::base()."user");
					}
				}
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
			
			
		}
	}

	public function action_datapribadi()
	{
		$user = Auth_ORM::instance()->get_user();
		$data = $user->mstpelamar;
		$countries = ORM::factory('country')->find_all()->as_array('id', 'country_name');
		$agamas = ORM::factory('mstagama')->find_all()->as_array('id', 'agama');
				
		$view = View::factory('user/datapribadi');
		$view->data = $data;
		$view->agamalist = $agamas;
		$view->countries = $countries;
		if(Request::current()->is_ajax() == $this->request)
		{
			echo $view;
		}
		else 
		{
			$this->template->container = $view;
		}		
		
	}
	
	public function action_data()
	{
		$user = Auth_ORM::instance()->get_user();
		$data = $user->mstpelamar;
				
		$view = View::factory('user/data');
		$view->data = $data;
		$view->captcha = Captcha::instance();
		if(Request::current()->is_ajax() == $this->request)
		{
			echo $view;
		}
		else 
		{
			$this->template->container = $view;
		}		
	}
}